package com.snow.useviewdemo.glide;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.snow.img.ImageFilletDirection;
import com.snow.img.ImageUtil;
import com.snow.img.ZoomImageView;
import com.snow.useviewdemo.R;
import com.snow.useviewdemo.util.LogUtils;
import com.snow.useviewdemo.util.TextViewUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * author : zyt
 * e-mail : 632105276@qq.com
 * date   : 2019-10-10
 * desc   :
 */
public class GlidePicActivity extends AppCompatActivity {
    @BindView(R.id.imageView01)
    ImageView imageView01;
    @BindView(R.id.imageView02)
    ImageView imageView02;
    @BindView(R.id.imageView03)
    ImageView imageView03;
    @BindView(R.id.imageView04)
    ImageView imageView04;
    @BindView(R.id.imageView05)
    ImageView imageView05;
    //    private final String TEST_IMAGE = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F611%2F031213123016%2F130312123016-3-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1646964747&t=20c4a59aff7bf7eeaeadfdc82ae222c5";
    private final String TEST_IMAGE = "https://p.scooper.news/1013928367931527168/2022-09-15/c4bd426ba40363747a86945208b83422.jpg";
    @BindView(R.id.imageView06)
    ImageView imageView06;
    @BindView(R.id.imageView07)
    ImageView imageView07;
    @BindView(R.id.imageView08)
    ImageView imageView08;
    @BindView(R.id.imageView09)
    ImageView imageView09;
    private String webpUrl = "https://rr.lygou.cc/v/JC0zPHN6aDMrJSk2aSAwMmkpKzcpdnYvcWEjdyAldD10KX8wfiAhcXo6cXtwNyNyIXF6bnN-Zzg3cX01cWhye3hicHxzdHk_xv_fallPreview_360_528_768?x-oss-process=image/format,webp/quality,Q_70&_h_=480&_w_=360";
    private String gifUrl = "http://jirili-oss.oss-cn-shanghai.aliyuncs.com/2020-05-13/1589375486557hq1ghk.gif";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_glide_pic);
        ButterKnife.bind(this);
        findViewById(R.id.refresh_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setImageData();
            }
        });
        setImageData();
    }

    private void setImageData() {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            imageView01.setOutlineProvider(new OutlineProvider(TextViewUtils.init().getDpValue(this, R.dimen.margin_015)));
            imageView01.setClipToOutline(true);
        }
//        //通过url获取到Drawable
        ImageUtil.imageLoadUrlToDrawable(this, TEST_IMAGE, new ImageUtil.LoadUrlToDrawableListener() {
            @Override
            public void imageDrawable(Drawable drawable) {
                imageView01.setBackgroundDrawable(drawable);
            }
        });
//        //通过URL加载获得Bitmap文件
        ImageUtil.imageLoadUrlToBitmap(this, TEST_IMAGE, new ImageUtil.LoadUrlToBitmapListener() {
            @Override
            public void imageBitmap(Bitmap resource) {
                imageView02.setImageBitmap(resource);
            }
        });
        //加载圆图片
        ImageUtil.imageLoadCircle(this, TEST_IMAGE, imageView03, R.drawable.bg_common_shadow);
        //加载各种圆角图片
        ImageUtil.imageLoadFillet(this, TEST_IMAGE, imageView04, num2Dip(this, 20), R.drawable.bg_common_shadow);
        ImageUtil.imageLoadFillet(this, TEST_IMAGE, imageView05, num2Dip(this, 20), ImageFilletDirection.Top, R.drawable.bg_common_shadow);
        ImageUtil.imageLoadFillet(this, TEST_IMAGE, imageView06, num2Dip(this, 20), ImageFilletDirection.Bottom, R.drawable.bg_common_shadow);
        ImageUtil.imageLoadFillet(this, TEST_IMAGE, imageView07, num2Dip(this, 20), ImageFilletDirection.Left, R.drawable.bg_common_shadow);
        ImageUtil.imageLoadFillet(this, TEST_IMAGE, imageView08, num2Dip(this, 20), ImageFilletDirection.Right, R.drawable.bg_common_shadow);


        //可缩放的图片
        ZoomImageView zoomImageView = findViewById(R.id.scale_iv);
        zoomImageView.setMaxScale(16f);
//        zoomImageView.setCanScale(false);
        ImageUtil.imageLoad(this, TEST_IMAGE, zoomImageView, R.drawable.bg_common_shadow);


        //glide加载webp格式的图片
        //https://github.com/zjupure/GlideWebpDecoder
        //加载webp格式圆图
//        Transformation<Bitmap> circleCrop = new CircleCrop();
//        Glide.with(this)
//                .load(webpUrl)
//                .optionalTransform(circleCrop)
//                .optionalTransform(WebpDrawable.class, new WebpDrawableTransformation(circleCrop))
//                .into(imageView09);
        //加载webp格式方图
        Glide.with(this).load(webpUrl).into(imageView09);
    }

    private void setImageViewScaleType() {
        //两个ScaleType切换发现ImageView.ScaleType.MATRIX无效
        imageView01.setScaleType(ImageView.ScaleType.CENTER_CROP);
        //需要这样处理，先设置setScaleType，在设置setImageMatrix才会生效，想显示长图的上半部分
        imageView01.setScaleType(ImageView.ScaleType.MATRIX);
        Matrix matrix = new Matrix();
        imageView01.setImageMatrix(matrix);
    }
    /**
     * 数字转化为dp
     *
     * @param num
     * @return
     */
    public int num2Dip(Context mContext, int num) {
        return mContext.getResources().getDimensionPixelSize(R.dimen.base_dip) * num;
    }
}
